package com.wc.alorithm_luogu.P4924;

import java.util.Scanner;

/**
 * @Author congge
 * @Date 2023/4/25 17:33
 * @description https://www.luogu.com.cn/problem/P4924
 * 魔法少女小Scarlet（原地移动）
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] map = new int[n][n];

        int num = 1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                map[i][j] = num++;
            }
        }

        while (m-- > 0) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            int r = sc.nextInt();
            int z = sc.nextInt();
            if (z == 0) {
                splayClockwise(map, x - 1, y - 1, r);
            } else {
                splayAntiClockwise(map, x - 1, y - 1, r);
            }
        }

        StringBuilder ans = new StringBuilder();
        for (int i = 0; i < n; i++) {
            ans.append(map[i][0]);
            for (int j = 1; j < n; j++) {
                ans.append(' ');
                ans.append(map[i][j]);
            }
            ans.append('\n');
        }
        System.out.println(ans);
    }

    // 顺时针
    public static void splayClockwise(int[][] map, int x, int y, int r) {
        int up = x - r;
        int down = x + r;
        int left = y - r;
        int right = y + r;

        int tmp;
        // 旋转r次
        for (int i = 0; i < r; i++) {
            // 顺时针旋转
            for (int j = left + i, d = 0; j < right - i; j++, d++) {
                tmp = map[up][left + d + i];
                map[up][left + d + i] = map[down - d][left + i];
                map[down - d][left + i] = map[down][right - i - d];
                map[down][right - i - d] = map[up + d][right - i];
                map[up + d][right - i] = tmp;
            }
            up++;
            down--;
        }
    }

    // 逆时针
    public static void splayAntiClockwise(int[][] map, int x, int y, int r) {
        int up = x - r;
        int down = x + r;
        int left = y - r;
        int right = y + r;

        int tmp;
        // 旋转r次
        for (int i = 0; i < r; i++) {
            // 逆时针时针旋转
            for (int j = left + i, d = 0; j < right - i; j++, d++) {
                tmp = map[up][left + d + i];
                map[up][left + d + i] = map[up + d][right - i];
                map[up + d][right - i] = map[down][right - i - d];
                map[down][right - i - d] = map[down - d][left + i];
                map[down - d][left + i] = tmp;
            }
            up++;
            down--;
        }
    }
}
